import time
import pandas as pd
import numpy as np
import openpyxl
import re
import os
import logging
import math
from collections import defaultdict
from openpyxl import load_workbook
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import Select
from urllib.parse import urljoin

#https://www.jianshu.com/p/56722a33ad07
#https://zhuanlan.zhihu.com/p/599579339




class PachongCNKIHighSearch:
    def __init__(self):
        self.FileCompany = '爬虫数据/CNKI论文信息/科技型企业信息CNKI_删除1-2年v1.2.xlsx'  # 文件夹路径，替换为你的实际文件夹路径
        logging.basicConfig(filename='爬虫数据/CNKI论文信息/CNKI论文导出.log', level=logging.INFO,
                            format='%(asctime)s - %(levelname)s - %(message)s')

    def pachongBegin(self):
        #get直接返回，不再等待界面加载完成
        desired_capabilities = DesiredCapabilities.CHROME
        desired_capabilities["pageLoadStrategy"] = "none"
        # 设置谷歌驱动器的环境
        options = webdriver.ChromeOptions()
        # 设置chrome不加载图片，提高速度
        # options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2})
        # 设置不显示窗口
        #options.add_argument('--headless')
        # 创建一个谷歌驱动器
        self.driver = webdriver.Chrome(options=options)
        # 最大化浏览器窗口
        self.driver.maximize_window()
        # 打开页面
        self.driver.get("https://kns.cnki.net/kns8/AdvSearch?crossDbcodes=CJFQ%2CCDMD%2CCIPD%2CCCND%2CCISD%2CSNAD%2CBDZK%2CCCJD%2CCCVD%2CCJFN")
        # 点击专业搜索
        WebDriverWait(self.driver, 50).until(
            EC.presence_of_element_located((By.CSS_SELECTOR, '[name="majorSearch"]'))).click()
        # WebDriverWait( self.driver, 50 ).until( EC.presence_of_element_located( (By.XPATH ,"/html/body/div[4]/div/div[2]/ul/li[3]") ) ).click()
        # print("点击专业搜索完成。")
        time.sleep(3)
        dateBox0=WebDriverWait( self.driver, 100 ).until( EC.presence_of_element_located( (By.CSS_SELECTOR, '#datebox0') ) )#.send_keys("2012-01-01")
        date = "2011-01-01"
        self.driver.execute_script(f"arguments[0].setAttribute('value', '{date}')", dateBox0)
        dateBox1=WebDriverWait( self.driver, 100 ).until( EC.presence_of_element_located( (By.CSS_SELECTOR, '#datebox1') ) )#.send_keys("2012-01-01")
        date1 = "2022-12-31"
        self.driver.execute_script(f"arguments[0].setAttribute('value', '{date1}')", dateBox1)
        # print("输入日期完成。")
        WebDriverWait(self.driver, 100).until(
            EC.presence_of_element_located((By.TAG_NAME, 'textarea'))).send_keys(
            "AF={theme}".format(theme="康佳集团股份有限公司"))
        WebDriverWait(self.driver, 50).until(EC.presence_of_element_located(
            (By.XPATH, "/html/body/div[2]/div/div[2]/div/div[1]/div[1]/div[2]/div[2]/input"))).click()
        time.sleep(3)
        WebDriverWait(self.driver, 50).until(EC.presence_of_element_located(
            (By.XPATH, "//div[@id='perPageDiv']"))).click()
        WebDriverWait(self.driver, 50).until(EC.presence_of_element_located(
            (By.XPATH, "/html/body/div[3]/div[2]/div[2]/div[2]/form/div/div[1]/div[2]/div[2]/div/div/ul/li[3]"))).click()

    def PaperSingelCompany(self,theme,lenlen):
        '''

        '''
        #输入检索表达式，以作者单位搜索
        # theme = "西安标准工业股份有限公司"
        WebDriverWait(self.driver, 100).until(EC.presence_of_element_located((By.TAG_NAME, 'textarea'))).clear()
        text = "AF=" + " OR AF=".join(theme[:len(theme)])
        WebDriverWait(self.driver, 100).until(
            EC.presence_of_element_located((By.TAG_NAME, 'textarea'))).send_keys(
            text)
        print("text=",text)
        logging.info("专业检索输入字符串：%s", text)
        WebDriverWait( self.driver, 50 ).until( EC.presence_of_element_located( (By.XPATH ,"/html/body/div[2]/div/div[2]/div/div[1]/div[1]/div[2]/div[2]/input") ) ).click()
        time.sleep(5)
        # print("点击检索完成。")

        # 发表年度框点开判断
        # checkboxYear =WebDriverWait(self.driver, 100).until(
        #     EC.presence_of_element_located((By.XPATH, "/html/body/div[5]/div[2]/div[1]/div[3]/dl[3]")))
        # # 获取 checkboxYear 的 class 属性值
        # checkbox_class = checkboxYear.get_attribute("class")
        # # 判断checkbox是否被选中
        # if "is-up-fold" in checkbox_class and "off" in checkbox_class:
        #     # print("元素被折叠。")
        #     checkboxYear.click()
        # else:
        #     pass
        #     # print("元素未被折叠。")
        # time.sleep(5)

        result_matrix=self.PaperDaoChu()
        time.sleep(1)
        return result_matrix #paper_num,confer_num,standerd_num

    def PaperDaoChu(self):

        paperAllCnt = WebDriverWait(self.driver, 50).until(
            EC.presence_of_element_located((By.XPATH, "//a[@data-id='all' and @class='all']"))).text
        #  paperAllCnt   总库\n38
        paperAllCnt = re.findall(r'\d+', paperAllCnt)
        if len(paperAllCnt) != 0:
            paperAllCnt = int(paperAllCnt[0])
        else:
            paperAllCnt = 0
        logging.info("2011-2022发文总量：%s", paperAllCnt)
        print('paperAllCnt', paperAllCnt)
        # time.sleep(50000)
        # if paperAllCnt>500:
        #     print("论文数量为：%s，超导出数量。",paperAllCnt)
        #     # return -1
        # 清除已选
        time.sleep(2)
        WebDriverWait(self.driver, 100).until(
            EC.presence_of_element_located((By.XPATH,
                                            "/html/body/div[3]/div[2]/div[2]/div[2]/form/div/div[1]/div[2]/div[1]/a"))).click()
        if paperAllCnt > 0 and paperAllCnt < 6001:  #
            dateYear = []
            quoteCnt = []
            downloadCnt = []
            i = 0
            while i < math.ceil(paperAllCnt / 50):
                if i==10:
                    break
                if i != 0 and i<10:
                    # page=0
                    # while i!=page:
                    WebDriverWait(self.driver, 50).until(
                        EC.presence_of_element_located((By.XPATH, "//a[@id='PageNext']"))).click()
                    time.sleep(3)
                    pagestr = WebDriverWait(self.driver, 50).until(
                        EC.presence_of_element_located(
                            (By.XPATH, "/html/body/div[3]/div[2]/div[2]/div[2]/form/div/div[1]/div[1]/span[2]"))).text
                    logging.info("翻页校对，预期：%s，实际：%s 。", i+1, pagestr)
                    print("翻页：", "i", i + 1, "page", pagestr)
                checkboxYear = WebDriverWait(self.driver, 100).until(
                EC.presence_of_element_located((By.XPATH,
                                                "/html/body/div[3]/div[2]/div[2]/div[2]/form/div/div[1]/div[2]/div[1]/label/input"))).click()
                i = i + 1
                time.sleep(2)
            # 搜索页面点击导出
            WebDriverWait(self.driver, 100).until(
                EC.presence_of_element_located((By.XPATH,
                                                "/html/body/div[3]/div[2]/div[2]/div[2]/form/div/div[1]/div[2]/ul[1]/li[2]/a"))).click()
            WebDriverWait(self.driver, 100).until(
                EC.presence_of_element_located((By.XPATH,
                                                "/html/body/div[3]/div[2]/div[2]/div[2]/form/div/div[1]/div[2]/ul[1]/li[2]/ul/li[1]"))).click()
            WebDriverWait(self.driver, 100).until(
                EC.presence_of_element_located((By.XPATH,
                                                "/html/body/div[3]/div[2]/div[2]/div[2]/form/div/div[1]/div[2]/ul[1]/li[2]/ul/li[1]/ul/li[12]/a"))).click()
            #导出页面
            if paperAllCnt<300:
                time.sleep(6)
            else:
                time.sleep(8)
            self.driver.switch_to.window(self.driver.window_handles[-1])
            # 使用Selenium的方法找到目标<ul>元素，这里以XPath为例
            ul_element = WebDriverWait(self.driver, 50).until(
                EC.presence_of_element_located((By.XPATH, "/html/body/div[3]/div/div[2]/div[2]/div")))
            # 使用len()函数获取<ul>元素的子元素个数
            date_elements = ul_element.find_elements(By.XPATH, ".//input[contains(@type, 'checkbox')]")
            # 遍历后代元素并输出文本内容
            for element in date_elements:
                element.click()
            time.sleep(2)
            #导出xls
            WebDriverWait(self.driver, 50).until(
                EC.presence_of_element_located((By.XPATH, "/html/body/div[3]/div/div[2]/div[1]/ul/li[6]/a"))).click()
            if paperAllCnt < 300:
                time.sleep(4)
            else:
                time.sleep(8)
            # 在新页面上的操作完成后，关闭当前页面
            self.driver.close()
            # 切换回上一页面的窗口句柄
            self.driver.switch_to.window(self.driver.window_handles[0])
            # 清除已选
            WebDriverWait(self.driver, 100).until(
                EC.presence_of_element_located((By.XPATH,
                                                "/html/body/div[3]/div[2]/div[2]/div[2]/form/div/div[1]/div[2]/div[1]/a"))).click()
            time.sleep(2)
        return paperAllCnt

    def rename_file(self, name1,name2,paperAllCnt,comCount):
        folder = r"C:/Users/Administrator/Downloads/"
        names = os.listdir(folder)
        #print(names[0][0:4])
        for i in range(len(names)):
            if names[i][0]!='Z':
                os.rename(folder + names[i],
                          folder + '已改名/' + 'Z_CNKI_20230917_{name1}-{name2}_{paperAllCnt}_{comCount}.xls'.format(name1=name1,
                                                                                                          name2=name2,
                                                                                                          paperAllCnt=paperAllCnt,
                                                                                                          comCount=comCount))
                return 1
        return 0
    def PaperYearCiteDownload(self):

        paperAllCnt = WebDriverWait(self.driver, 50).until(
            EC.presence_of_element_located((By.XPATH, "//a[@data-id='all' and @class='all']"))).text
        #  paperAllCnt   总库\n38
        paperAllCnt = re.findall(r'\d+', paperAllCnt)
        if len(paperAllCnt) != 0:
            paperAllCnt = int(paperAllCnt[0])
        else:
            paperAllCnt = 0
        logging.info("2011-2022发文总量：%s", paperAllCnt)
        print('paperAllCnt',paperAllCnt)
        if paperAllCnt>0 and paperAllCnt<6001: #
            dateYear = []
            quoteCnt = []
            downloadCnt = []
            i=0
            while i<math.ceil(paperAllCnt /50):
                if i!=0:
                    # page=0
                    # while i!=page:
                    WebDriverWait(self.driver, 20).until(
                        EC.presence_of_element_located((By.XPATH,"//a[@id='PageNext']"))).click()
                    time.sleep(5)
                    pagestr = WebDriverWait(self.driver, 20).until(
                        EC.presence_of_element_located((By.XPATH, "/html/body/div[5]/div[2]/div[2]/div[2]/form/div/div[1]/div[1]/span[2]"))).text
                    logging.info("翻页校对，预期：%s，实际：%s 。", i,pagestr)
                    print("翻页：","i",i+1,"page",pagestr)

                # 使用Selenium的方法找到目标<ul>元素，这里以XPath为例
                ul_element = WebDriverWait(self.driver, 50).until(
                    EC.presence_of_element_located((By.XPATH, "/html/body/div[5]/div[2]/div[2]/div[2]/form/div/table/tbody")))
                # 使用len()函数获取<ul>元素的子元素个数
                date_elements = ul_element.find_elements(By.XPATH, ".//td[contains(@class, 'date')]")
                # 遍历后代元素并输出文本内容
                for element in date_elements:
                    dateYear.append(element.text[:4])

                date_elements = ul_element.find_elements(By.XPATH, ".//td[contains(@class, 'quote')]")
                for element in date_elements:
                    quoteCnt.append(element.text)

                date_elements = ul_element.find_elements(By.XPATH, ".//td[contains(@class, 'download')]")
                for element in date_elements:
                    downloadCnt.append(element.text)
                i=i+1
            mat =np.transpose(np.array([ dateYear, quoteCnt, downloadCnt]))

            int_matrix = [[int(cell) if cell else 0 for cell in row] for row in mat]
            int_matrix = np.array(int_matrix)

            years = list(range(2011, 2023))
            result_matrix = []

            for year in years:
                year_stats = [year, 0, 0, 0]
                for row in int_matrix:
                    if row[0] == year:
                        year_stats[1] += 1
                        year_stats[2] += row[1]
                        year_stats[3] += row[2]
                result_matrix.append(year_stats)
            result_matrix=np.array(result_matrix)

            print(result_matrix)
        elif paperAllCnt==0:
            years = np.arange(2011, 2023)
            result_matrix = np.zeros((len(years), 4))
            result_matrix[:, 0] = years
        else:
            years = np.arange(2011, 2023)
            result_matrix = np.full((12, 4), 99999)
            result_matrix[:, 0] = years
            logging.info( "论文数量过大，无法爬虫，需人工输验证码。")
            print( "论文数量过大，无法爬虫，需人工输验证码。")

        return result_matrix

    def PaperSingeYearCount(self):
        #
        try:
            # 使用Selenium的方法找到目标<ul>元素，这里以XPath为例
            ul_element = WebDriverWait(self.driver, 5).until(
                        EC.presence_of_element_located((By.XPATH, "/html/body/div[5]/div[2]/div[1]/div[3]/dl[3]/dd/div/ul")))
            # 使用len()函数获取<ul>元素的子元素个数
            ul_length = len(ul_element.find_elements(By.TAG_NAME,"li"))
            # # 输出<ul>元素的子元素个数
            # print("ul元素的子元素个数为:", ul_length)
        except Exception as e:
            ul_length=0
        if ul_length>10:
            print(f"ul_length={ul_length}")
            time.sleep(1)
            mpSelectInput=WebDriverWait(self.driver, 50).until(
                EC.presence_of_element_located((By.XPATH, "/html/body/div[5]/div[2]/div[1]/div[3]/dl[3]/dd/div/a"))).click()
            # ActionChains(self.driver).move_to_element(mpSelectInput).click(mpSelectInput).perform()
            time.sleep(1)
        paperAllYear = []
        for i in range(1,ul_length+1,1):
            try:
                paperYear=WebDriverWait(self.driver, 8).until(
                    EC.presence_of_element_located((By.XPATH, "/html/body/div[5]/div[2]/div[1]/div[3]/dl[3]/dd/div/ul/li[{i}]/a".format(i=i)))).text
                paperYearCount = WebDriverWait(self.driver, 4).until(
                    EC.presence_of_element_located((By.XPATH,
                                                    "/html/body/div[5]/div[2]/div[1]/div[3]/dl[3]/dd/div/ul/li[{i}]/span".format(i=i)))).text
                paperYear = int(paperYear)
                paperYearCount=int(paperYearCount[1:-1])
                paperAllYear.append([paperYear, paperYearCount])
            except Exception as e:
                print("错误",i,e)
                break
        paperAllYear=np.array(paperAllYear)
        paperAllYearFill=np.array([[year, 0] for year in range(2011, 2023)])
        for i in range(len(paperAllYearFill)):
            for j in range(len(paperAllYear)):
                if paperAllYearFill[i,0]== paperAllYear[j,0]:
                    paperAllYearFill[i, 1]=paperAllYear[j,1]
                    break
        print(paperAllYearFill[:,1])
        return paperAllYearFill[:,1]


    def PaperSingeYearWriteXlsx(self,start_column, start_row, data_matrix):
        '''
        :param filename: 写入文件名字
        :param column_letter: 要写入的列
        :param start_row: 开始的行
        :param data_list:  待写入的数据,类型为list
        :return:
        '''
        # 打开已存在的xlsx文件
        workbook = openpyxl.load_workbook(self.FileCompany)
        # 选择工作表
        sheet = workbook.active

        # 循环遍历二维矩阵的每个元素，写入到指定位置
        for row_index, value in enumerate(data_matrix, start=start_row):
            sheet.cell(row=row_index, column=start_column, value=value)

        # 保存修改后的xlsx文件
        workbook.save(self.FileCompany)
        print("已写入")

    def get_list_union(self,input_list):
        """
        获取列表元素的并集及元素数量

        参数:
            input_list: 输入的列表

        返回:
            list_union: 列表元素的并集列表
            unique_count: 列表元素的唯一值数量
        """
        list_union = list(set(input_list))
        unique_count = len(list_union)

        return list_union, unique_count

    def count_consecutive_elements(self,column):
        """
        计算列表中连续相同元素的个数

        参数:
            column: 输入的列数组

        返回:
            count: 连续相同元素的个数
        """
        count = 1
        max_count = 1

        for i in range(1, len(column)):
            if column[i] == column[i - 1]:
                count += 1
                max_count = max(max_count, count)
            else:
                count = 1
                break

        return max_count

    def PaperAllCompanyFanPa(self):
        dataCompany = self.Read_data()
        # for i in range(len(dataCompany)):
        #     if math.isnan(dataCompany[i, 3]):
        #         pass
        # print("检测中断位置",i)
        i = 15404  # 6475
        trycount = 0
        while i < len(dataCompany):
            j = 0

            try:
                self.pachongBegin()
                while j < 1000:
                    yearCount = self.count_consecutive_elements(dataCompany[i:, 0])
                    nameList, nameCount = self.get_list_union(dataCompany[i:i + yearCount, 2])
                    print(yearCount, nameList, nameCount, f"j={j}", f"i={i}")
                    logging.info("企业全名：%s，全名数量：%s，统计年数：%s，表中位置：%s，反爬虫计数：%s。", nameList, nameCount,
                                 yearCount, i + 2, j)
                    if yearCount > 2:
                        paperAllYearFill = self.PaperSingelCompany(nameList, nameCount)
                        paperAllYearFill = paperAllYearFill[12 - yearCount:, 1:]
                        # self.PaperSingeYearWriteXlsx(start_column=5, start_row=i + 2, data_matrix=paperAllYearFill)
                        self.write_to_matrix(start_column=6, start_row=i + 2, data_matrix=paperAllYearFill)
                    else:
                        print("表中年份小于3")
                        logging.info("表中年份小于3")
                    j = j + 1
                    i = i + yearCount
                    trycount = 0
                    if i >= len(dataCompany):
                        break
                self.pachongQuit()
            except Exception as e:
                trycount = trycount + 1
                if trycount >= 2:
                    i = i + yearCount
                print("爬虫重启", i, "trycount", trycount, e)
                logging.info("爬虫中断重启位置：%s", i)
    def PaperAllCompanyFanPaXiaodui(self):
        dataCompany = self.Read_data()
        # for i in range(len(dataCompany)):
        #     if math.isnan(dataCompany[i, 3]):
        #         pass
        # print("检测中断位置",i)
        i =0 #  6475
        ii=9
        trycount = 0
        posCnt=[823,1392,1709,1816,2032,2140,2620,2932,3220,12302,14722]
        while ii < len(posCnt):
            j = 0

            try:
                self.pachongBegin()
                while j < 1000:
                    i = posCnt[ii]
                    yearCount = self.count_consecutive_elements(dataCompany[i:, 0])

                    nameList, nameCount = self.get_list_union(dataCompany[i:i + yearCount, 2])
                    print(yearCount, nameList, nameCount,f"j={j}",f"i={i}")
                    logging.info("企业全名：%s，全名数量：%s，统计年数：%s，表中位置：%s，反爬虫计数：%s。", nameList, nameCount,yearCount,i+2,j)
                    if yearCount > 2:
                        paperAllYearFill = self.PaperSingelCompany(nameList, nameCount)
                        paperAllYearFill = paperAllYearFill[12 - yearCount:,1:]
                        # self.PaperSingeYearWriteXlsx(start_column=5, start_row=i + 2, data_matrix=paperAllYearFill)
                        self.write_to_matrix(start_column=6, start_row=i + 2, data_matrix=paperAllYearFill)
                    else:
                        print("表中年份小于3")
                        logging.info("表中年份小于3")
                    j=j+1
                    ii = ii + 1#yearCount
                    trycount=0
                    if i >= len(dataCompany):
                        break
                self.pachongQuit()
            except Exception as e:
                trycount = trycount+1
                if trycount>=2:
                    i = i + yearCount
                print("爬虫重启",i,"trycount",trycount,e)
                logging.info("爬虫中断重启位置：%s",i)
    def PaperAllCompanyDaoChu(self):
        dataCompany = self.Read_data()
        i = 0    # 6475
        trycount = 0
        iBegin = 0
        while i < len(dataCompany):
            j = 0
            try:

                self.pachongBegin()
                while j < 1000:
                    iBegin=i
                    count = 0
                    paperCount = 0
                    paperCountSum = 0
                    while paperCountSum<450:
                        yearCount = self.count_consecutive_elements(dataCompany[i:, 0])
                        paperCount=sum(dataCompany[i:i + yearCount, 3])
                        i=i+yearCount
                        paperCountSum=paperCountSum+paperCount
                        if paperCountSum>150:
                            if count==0:
                                pass
                            else:
                                i=i-yearCount
                            break
                        if i >= len(dataCompany):
                            break
                        count=count+1
                    if count>0 or ( count==0 and paperCountSum<=500):
                        nameList, nameCount = self.get_list_union(dataCompany[iBegin:i, 2])
                        print( f"j={j}",f"iBegin={iBegin}", f"i={i}",nameCount,dataCompany[iBegin, 0],dataCompany[i-1, 0])
                        logging.info("企业数量：%s，开始：%s，结束：%s，全名：%s，反爬虫计数：%s。", nameCount,iBegin,i,nameList, j)
                        logging.info("股票代码：%s - %s。", dataCompany[iBegin, 0], dataCompany[i - 1, 0])

                        folder_path = "C:\\Users\\Administrator\\Downloads"  # 替换为你要删除文件的文件夹路径

                        # 遍历文件夹内的所有文件
                        for filename in os.listdir(folder_path):
                            file_path = os.path.join(folder_path, filename)
                            if os.path.isfile(file_path):
                                try:
                                    os.remove(file_path)  # 删除文件
                                    print(f"已删除文件: {file_path}")
                                except Exception as e:
                                    print(f"删除文件时出错: {file_path}, 错误信息: {str(e)}")

                        paperAllCnt = self.PaperSingelCompany(nameList, nameCount)
                        flag_rename=0
                        while flag_rename==0:
                            flag_rename=self.rename_file(dataCompany[iBegin, 0],dataCompany[i-1, 0],paperAllCnt,nameCount)

                    else:
                        yearCount = self.count_consecutive_elements(dataCompany[:i-1, 0])
                        paperCount = sum(dataCompany[i-yearCount:i , 3])
                        print(f"i={i-yearCount}",dataCompany[i-yearCount, 2],"未导出",paperCount)
                        logging.info("企业：%s,表中位置：%s 未导出",i-yearCount,dataCompany[i-yearCount, 2])
                    j = j + 1
                    # i = i + yearCount
                    trycount = 0
                    if i >= len(dataCompany):
                        break
                self.pachongQuit()
            except Exception as e:
                trycount = trycount + 1
                if trycount >= 4:
                    i = i + yearCount
                else:
                    i=iBegin
                print("爬虫重启", i, "trycount", trycount)
                logging.info("爬虫中断重启位置：%s", i)
    def write_to_matrix(self, start_column, start_row, data_matrix):
        '''
        :param filename: 写入文件名字
        :param column_letter: 要写入的列
        :param start_row: 开始的行
        :param data_list:  待写入的数据,类型为list
        :return:
        '''
        # 打开已存在的xlsx文件
        workbook = openpyxl.load_workbook( self.FileCompany)
        # 选择工作表
        sheet = workbook.active
        # 循环遍历二维矩阵的每个元素，写入到指定位置
        for row_index, row in enumerate(data_matrix, start=start_row):
            for col_index, value in enumerate(row, start=start_column):
                sheet.cell(row=row_index, column=col_index, value=value)
        # 保存修改后的xlsx文件
        workbook.save( self.FileCompany)
        print("写入完成")
    def Read_data(self):
        dataFileCompany = pd.read_excel(self.FileCompany,\
                                  usecols=[0,1,3,5],\
                                        #,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17
                                  skiprows=[0], \
                                  header=None)

        dataFileCompany = np.array(dataFileCompany)
        dataFileCompany=self.convert_first_column_to_int(dataFileCompany)

        return dataFileCompany
    def convert_first_column_to_int(self,matrix_array):
        # Step 1: Extract the first column as a string array
        first_column_str = matrix_array[:, 0]

        # Step 2: Convert the string array to int array
        first_column_int = first_column_str.astype(int)

        # Step 3: Replace the first column in the original array with the int array
        matrix_array[:, 0] = first_column_int

        return matrix_array
    def pachongQuit(self):
        # 关闭浏览器
        self.driver.close()
if __name__ == "__main__":

    PachongCNKIHighSearch=PachongCNKIHighSearch()

    PachongCNKIHighSearch.PaperAllCompanyDaoChu()

    # PachongCNKIHighSearch.rename_file( 55, 85)
